<template>
  <view class="box">
    <wd-input
      v-model="inputVal"
      style="width: 100%"
      suffix-icon="arrow-down"
      :prefix-icon="showScanCode ? 'scan' : false"
      :focus="focus"
      ref="inputRef"
      :placeholder="placeholder"
      @clickprefixicon="clickprefixicon"
      @clicksuffixicon="clicksuffixicon"
      @clear="clearInput"
      @focus="inputFocus"
      @input="inputFnc"
    />
    <view class="closeIcon" @click="clearInput"><wd-icon name="close-circle" size="14px" /></view>

    <view class="list" v-if="showList">
      <view v-for="item in dataList" :key="item.id" @click="() => setVal(item)">
        <view class="listItem">{{ item.name }}</view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import * as ddcode from 'dingtalk-jsapi'

const inputVal = ref<string>('')
const inputRef = ref()
const dataList = ref([])
const showList = ref<boolean>(false)

const props = defineProps({
  columns: {
    type: Array,
    default: [],
  },
  placeholder: {
    type: String,
    default: '',
  },
  showScanCode: {
    type: Boolean,
    default: false,
  },
})

const emit = defineEmits(['getList', 'searchPickerData'])

// 前后缀图标点击事件
const clickprefixicon = () => {
  console.log('clickprefixicon')
  ddcode.biz.util.scan({
    type: String, // type 为 all、qrCode、barCode，默认是all。
    onSuccess: (data) => {
      console.log(data, 66)
    },
    onFail: function (err) {
      console.log(err, 777)
    },
  })
}
const clicksuffixicon = () => {
  console.log('clicksuffixicon')
}

// 输入框清楚事件
function clearInput() {
  if (!inputVal.value) {
    showList.value = false
    return
  }
  showList.value = false
  inputVal.value = ''
  emit('getList')
}

// 列表点击事件
function setVal(data) {
  inputVal.value = data.name
  showList.value = false
  emit('getList')
}

// 输入框聚焦事件
function inputFocus(data) {
  if (!showList.value) {
    showList.value = true
  }
}

// 输入框输入事件
function inputFnc() {
  emit('searchPickerData')
}

onMounted(() => {
  const list = []
  for (let i = 0; i < 10; i++) {
    const obj = {
      id: i,
      name: 'name' + i,
    }
    list.push(obj)
  }
  dataList.value = list
})
</script>

<style scoped lang="scss">
.box {
  width: 100%;
  position: relative;
  .closeIcon {
    position: absolute;
    right: 5%;
    top: 8px;
    z-index: 10;
  }
  .list {
    box-sizing: border-box;
    position: absolute;
    top: 56px;
    left: 0;
    width: 80%;
    margin: auto;
    left: 0;
    right: 0;
    padding: 10px;
    max-height: 200px;
    overflow-y: scroll;
    background-color: #ffffff;
    border-radius: 6px;
    box-shadow:
      #dddddd 4px 4px 8px,
      #dddddd -4px -4px 8px;
    z-index: 2;
    .listItem {
      padding: 8px 0px;
      box-sizing: border-box;
      border-bottom: solid 1px #dddddd;
    }
  }
  .list::before {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-bottom: solid 6px #ffffff;
    border-right: solid 6px transparent;
    border-left: solid 6px transparent;
    left: 50%;
    top: -6px;
    margin-left: -6px;
    z-index: 6;
  }
}
</style>
